package com.mx.kn.cstf2.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;

import kn.cstf2.transition.Bean.PersonalBean;
import kn.cstf2.transition.Bean.TransitionBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.lowagie.text.List;
import com.mx.kn.cstf2.dao.CopDao;
import com.mx.kn.cstf2.dao.MailDao;
import com.mx.kn.cstf2.dao.TransitionDao;
import com.mx.kn.cstf2.dto.CatalogoBranchDto;
import com.mx.kn.cstf2.dto.CopContactsDto;
import com.mx.kn.cstf2.dto.CorreosBranchDto;
import com.mx.kn.cstf2.service.CopService;
import com.mx.kn.cstf2.service.MailService;
import com.mx.kn.cstf2.utils.PlantillaNotifyStatus;
@Service
public class MailServiceImpl implements MailService{

	@Autowired TransitionDao transitionDao;
	@Autowired MailDao mailDao;
	@Autowired CopDao copDao;
	
	@Override
	public void sendNotifyStatusMail(TransitionBean transisition, int plantilla){
		//obtener correos 
		ArrayList<String> mails = new ArrayList<String>();

		//correos personal
		try {
			for (PersonalBean persona : transitionDao.obtenerDataPersonalByIdPersonal(transisition.getId())) {
								mails.add(persona.getEmail());
			}
			//correos  branch
			for (CatalogoBranchDto catalogo : transitionDao.obtieneBranchAndMails(transisition)) {
				for(CorreosBranchDto correo  : catalogo.getCorreosBranch()){
					mails.add(correo.getCorreos());
				}
				
			}
			
			//esto cambiara de acuerdo a lo que te diga Hipster
			if(PlantillaNotifyStatus.NOTIFY_IMPLEMENTATION == plantilla){
				//caso para implementacion
				mailDao.sendNotifyStatusMail(transitionDao.obtenerTransitionBeanByKnkeyAndId(transisition), mails);
			}else if(PlantillaNotifyStatus.NOTIFY_TRANSITION == plantilla){
				//caso para transition
				mailDao.sendNotifyStatusMail(transitionDao.obtenerTransitionBeanByKnkeyAndId(transisition), mails);
			}else if(PlantillaNotifyStatus.NOTIFY_COP == plantilla){
				for(CopContactsDto contacto  : copDao.getContactsListMail(transisition.getId())){
					mails.add(contacto.getEmail());
				}
				mailDao.sendNotifyStatusMail(transitionDao.obtenerTransitionBeanByKnkeyAndId(transisition), mails);
				
			}
			
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	
	}

	@Override
	public void sendNotifyPersonsGrid(ArrayList<PersonalBean> gridPersonas)
			throws SQLException {
		mailDao.sendNotifyPersonsGrid(gridPersonas);
		
	}

	@Override
	public java.util.List<CopContactsDto> getNotifyStatusCop(Integer idCop)
			throws SQLException {
		
		return copDao.getNotifyStatusCop(idCop);
	}

	@Override
	public void sendNotifyContactsCop(java.util.List<CopContactsDto> correos)
			throws SQLException {
		mailDao.sendNotifyContactsCop(correos);
		
	}

}
